<?php

namespace App\Http\Controllers;

use App\Models\OpenAPI;
use App\Models\Predict;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class OpenAPIsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $apis = OpenAPI::with(['user'])->requestCriteria($request)->paginate($request->query('limit'));
        return $this->respondSuccess($apis);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = [
            'user_id' => $request->user_id,
            'api_token' => 'GuanGai-' . Str::random(56),
        ];
        $api = OpenAPI::create($data);
        return $this->respondSuccess($api);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\OpenAPI  $openAPI
     * @return \Illuminate\Http\Response
     */
    public function show(OpenAPI $openapi)
    {
        return $this->respondSuccess($openapi);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\OpenAPI  $openAPI
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, OpenAPI $openapi)
    {
        $data = [
            'api_token' => 'GuanGai-' . Str::random(56),
        ];
        if ($request->user()->hasRole('超级管理员') && $request->has('valid')) {
            $data['valid'] = $request->valid;
        }
        $result = $openapi->update($data);
        return $this->respondSuccess($openapi);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\OpenAPI  $openAPI
     * @return \Illuminate\Http\Response
     */
    public function destroy(OpenAPI $openapi)
    {
        $data = $openapi->delete();
        return $this->respondSuccess($data);
    }

    public function predict_result(Request $request)
    {
        $this->validate($request, [
            'dev_ids' => 'required|array|max:50',
        ]);
        $dev_ids=$request->input('dev_ids');
        $data = Predict::whereIn('dev_id', $dev_ids)->first();
        return $this->respondSuccess($data);
    }

    public function predict_history(Request $request)
    {
        $this->validate($request, [
            'dev_ids' => 'required|array|max:50',
            'date_begin' => 'required|min:10|max:19',
            'date_end' => 'required|min:10|max:19',
        ]);
        $dev_ids = $request->input('dev_ids');
        $begin = $request->input('date_begin'); 
        $end = $request->input('date_end'); 
        $data = Predict::whereIn('dev_id', $dev_ids)->whereBetween('created_at', [$begin, $end])
            ->requestCriteria($request)->paginate($request->query('limit'));
        return $this->respondSuccess($data);
    }
}
